/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = wtc_typed_string.c                                 */
/*  NOTE      =                                                    */
/*  DATE      = 2015/08/26 by songbb                               */
/*******************************************************************/

#include "frreport_comm.h"
#include "frct_comm.h"

#include "../wtc_core.h"
#include "../wtc_comm.h"
#include "../wtc_mar.h"
#include "wtc_typed_string.h"

int wtc_typed_string_tmpostrecv(Conntrack *p_st_conn
                                , L7session_wtc *p_st_sess
                                , L7_wtc_packet *p_st_wtc_packet
                                , int i_recv_size
                                , Frreport_body_data *p_st_body)
{
    WTC_PRINT_INFO("wtc_typed_string_tmpostrecv...\n");
    L7_session *p_st_session = frct_get_session(p_st_conn);
    L7session_wtc_private *p_st_session_private = (L7session_wtc_private *) frct_get_session_private(p_st_session);
    L7_wtc_stmt *p_st_wtc_stmt = p_st_session_private->p_st_wtc_stmt;
    char *p_string = NULL;
    unsigned int ui_string_len = 0;
    char *p_field_name = NULL;
    int i_field_name_len = 0;

    if (wtc_mar_decode_string(p_st_sess, NULL, 0, p_st_wtc_packet, &p_string, &ui_string_len) != FR_SUCC)
        return FR_FAIL;

    if (p_st_body != NULL)
    {
        i_field_name_len = strlen(p_st_wtc_packet->i_is_req == FR_YES ? APM_DTS_REPORT_REQ_ARG_DATA : APM_DTS_REPORT_RESP_ARG_DATA) + 2;
        if ((p_field_name = (char *) frmalloc(i_field_name_len * sizeof(char))) == NULL)
        {
            WTC_MALLOCK_ERR;
            goto error;
        }
        snprintf(p_field_name, (size_t) i_field_name_len, "%s%d", p_st_wtc_packet->i_is_req == FR_YES ? APM_DTS_REPORT_REQ_ARG_DATA
                                                                                                      : APM_DTS_REPORT_RESP_ARG_DATA, 1);

        if (frreport_body_l7_add_str(p_st_body, p_field_name, p_string) != FR_SUCC)
            goto error;
        p_st_wtc_stmt->ui_arg_count++;
    }

    wtc_free((void **) &p_string);
    wtc_free((void **) &p_field_name);
    return FR_SUCC;
error:
    wtc_free((void **) &p_string);
    wtc_free((void **) &p_field_name);
    return FR_FAIL;
}

int wtc_typed_string_tmpostrecv_fad(int i_recv_size
                                    , char *p_payload
                                    , unsigned int ui_payload_len
                                    , unsigned int *p_ui_offset)
{
    WTC_PRINT_INFO("wtc_typed_string_tmpostrecv...\n");
    return wtc_skip_string(p_payload, ui_payload_len, p_ui_offset);
}
